<html>
<head>
    <title>我是消息发送者</title>
</head>

<body>
    <H1> Tom</H1>

    <div class="video_container" align="center">
        <video id="local_video"  poster="img/video_fill.jpg" autoplay muted></video>
    </div>
    <hr>
    <button id="startButton">获取本地视频</button>
    <!-- <button id="callButton">呼叫bob</button> -->
    <button id="hangupButton">挂断</button>


    <button id="calljerrybtn">call Jerry</button>

    <script src="/socket.io/socket.io.js"></script>
    <script src="./js/adapter-latest.js"></script>
    <script>
        var socket=io();

        var localVideo = document.getElementById('local_video');

var startButton = document.getElementById('startButton');
var callButton = document.getElementById('callButton');
var hangupButton = document.getElementById('hangupButton');

var pc;
var localStream;


var config = {
    'iceServers': [
    //     {
    //     'urls': 'stun:stun.l.google.com:19302'
    // }
    
    {
        "urls": "stun:192.168.1.16:3478"
    },{
        "urls": "stun:192.168.1.16:3478",
             "username":"test",
             "credential":"123456"
    }


    ]
};



const offerOptions = {
    offerToReceiveVideo: 1,
    offerToReceiveAudio: 1
};

startButton.addEventListener('click', startAction);
hangupButton.addEventListener('click', hangupAction);

         var calljerrybutton=document.getElementById('calljerrybtn');
        socket.emit('assignusername','tom');
        socket.emit('joinRoom',"room1");


     calljerrybutton.addEventListener('click',()=>{
         socket.emit('calljerry',"testmessage");

         callAction();

     })


     function startAction() {
    navigator.mediaDevices.getUserMedia({ video: true, audio: true }).then(function (mediastream) {
        localStream = mediastream;
        localVideo.srcObject = mediastream;
        startButton.disabled = true;
    }).catch(function (e) {
        console.log(JSON.stringify(e));
    });
}



function hangupAction() {

localStream.getTracks().forEach(track => track.stop());
pc.close();
pc = null;
// hangupButton.disabled = true;
// callButton.disabled = true;
// startButton.disabled = false;

}



function callAction() {
    //callButton.disabled = true;
    //hangupButton.disabled = false;
    pc = new RTCPeerConnection(config);
    localStream.getTracks().forEach(track => pc.addTrack(track, localStream));
    pc.createOffer(offerOptions).then(function (offer) {
        pc.setLocalDescription(offer);
        socket.emit('signal', offer);
    });
    pc.addEventListener('icecandidate', function (event) {
        var iceCandidate = event.candidate;
        if (iceCandidate) {
            socket.emit('ice', iceCandidate);
        }
    });
}


socket.on('signal', function (message) {
    if (pc !== 'undefined') {
        pc.setRemoteDescription(new RTCSessionDescription(message));
        console.log('remote answer');
    }
});

socket.on('ice', function (message) {
    if (pc !== 'undefined') {
        pc.addIceCandidate(new RTCIceCandidate(message));
        console.log('become candidate');
    }
});


    </script>

</body>


</html>